#################################################################################### 
####################################################################################
####################################################################################
# Appendix L: Tables L.38-L.41
####################################################################################
####################################################################################
library(here)
library(kableExtra)
library(lmtest)
library(multiwayvcov)
####################################################################################
####################################################################################
## Read in survey data (from: 8_2020_analysis_charity.R)
dat = readRDS(here("data/dat_unrestricted.rds"))
datb = readRDS(here("data/dat_restricted.rds"))

# Implement SWE of ATE: Demean all covariates, fully interact
cols = c("sex","age","education_level","Arua","Bushenyi","Ibanda","Jinja","Mbale","Mpigi","Pallisa","Mbarara","Shema",
         "village_distance_HF","village_distance_HOSP","village_distance_road","village_distance_electricity", "share2006")
dat[, paste0("c_", cols)] = lapply(dat[, cols], function(x) (x - mean(x))  )
covariates = c("treatment*c_village_distance_HF + treatment*c_village_distance_HOSP + treatment*c_village_distance_road + treatment*c_village_distance_electricity + 
                treatment*c_sex + c_age*treatment + c_education_level*treatment + treatment*c_share2006 + 
                c_Arua*treatment + c_Bushenyi*treatment + c_Ibanda*treatment + c_Jinja*treatment + c_Mbale*treatment + c_Mpigi*treatment + c_Pallisa*treatment + c_Mbarara*treatment",
               "c_Arua*treatment + c_Bushenyi*treatment + c_Ibanda*treatment + c_Jinja*treatment + c_Mbale*treatment + c_Mpigi*treatment + c_Pallisa*treatment + c_Mbarara*treatment")

# Implement SWE of ATE: Demean all covariates, fully interact
cols = c("sex","age","education_level","Arua","Ibanda","Pallisa","Shema",
         "village_distance_HF","village_distance_HOSP","village_distance_road","village_distance_electricity", "share2006")
datb[, paste0("c_", cols)] = lapply(datb[, cols], function(x) (x - mean(x))  )
covariatesb = c("treatment*c_village_distance_HF + treatment*c_village_distance_HOSP + treatment*c_village_distance_road + treatment*c_village_distance_electricity + 
                treatment*c_sex + c_age*treatment + c_education_level*treatment + treatment*c_share2006 + 
                c_Arua*treatment + c_Ibanda*treatment + c_Pallisa*treatment", 
                "c_Arua*treatment + c_Ibanda*treatment + c_Pallisa*treatment")


####################################################################################
####################################################################################
## Received Care
dat$charity = 0
dat[!is.na(dat$charity_for_profit) & dat$charity_for_profit == 1 & dat$treatment == 1,]$charity = 1
dat$treatment = dat$charity

####################################################################################
####################################################################################
## Received Care
datb$charity = 0
datb[!is.na(datb$charity_for_profit) & datb$charity_for_profit == 1 & datb$treatment == 1,]$charity = 1
datb$treatment = datb$charity


####################################################################################
####################################################################################
## Expectations (Who should provide Health Services) ####

# Unrestricted
for (i in names(dat[c("health_care_groups_st","health_care_pay_st","donate_lg_st","expectations_index")])){ # Dependent Variables
  for (j in covariates) {
    model = paste(i,"~","treatment","+", j)
    # Run each model
    assign(x = paste("m",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = dat))
    # Output clustered SEs (county)
    assign(x = paste("c",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = dat),
                            cluster.vcov(lm(as.formula(model), data = dat), dat$village_id)))
  }
}

# Restricted
for (i in names(datb[c("health_care_groups_st","health_care_pay_st","donate_lg_st","expectations_index")])){ # Dependent Variables
  for (j in covariatesb) {
    model = paste(i,"~","treatment","+", j)
    # Run each model
    assign(x = paste("bm",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = datb))
    # Output clustered SEs (county)
    assign(x = paste("bc",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = datb),
                            cluster.vcov(lm(as.formula(model), data = datb), datb$village_id)))
  }
}

####################################################################################
####################################################################################
## Table L.38

stargazer(m.expectations_index.t, bm.expectations_index.t, m.donate_lg_st.t, bm.donate_lg_st.t, 
          m.health_care_groups_st.t, bm.health_care_groups_st.t, m.health_care_pay_st.t, bm.health_care_pay_st.t,  
          
          se = list(c.expectations_index.t[,2], bc.expectations_index.t[,2], c.donate_lg_st.t[,2], bc.donate_lg_st.t[,2], 
                    c.health_care_groups_st.t[,2], bc.health_care_groups_st.t[,2], c.health_care_pay_st.t[,2], bc.health_care_pay_st.t[,2] ),
          
          p = list(c.expectations_index.t[,4], bc.expectations_index.t[,4], c.donate_lg_st.t[,4], bc.donate_lg_st.t[,4], 
                   c.health_care_groups_st.t[,4], bc.health_care_groups_st.t[,4], c.health_care_pay_st.t[,4], bc.health_care_pay_st.t[,4] ),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/expectations_1_c_charity.tex",
          label = "tab:expectations_1_c",column.sep.width = "1pt", table.placement = "!ht",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Preferences for NGO Service Provision",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", dep.var.caption = "", table.placement = "!ht",notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Index","Donation","Provision","Payment"), column.separate = c(2,2,2,2),
          add.lines = list(c("Restricted", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes")))

####################################################################################
####################################################################################
## Table L.39

stargazer(m.expectations_index.c, bm.expectations_index.c, m.donate_lg_st.c, bm.donate_lg_st.c, 
          m.health_care_groups_st.c, bm.health_care_groups_st.c, m.health_care_pay_st.c, bm.health_care_pay_st.c,  
          
          se = list(c.expectations_index.c[,2], bc.expectations_index.c[,2], c.donate_lg_st.c[,2], bc.donate_lg_st.c[,2], 
                    c.health_care_groups_st.c[,2], bc.health_care_groups_st.c[,2], c.health_care_pay_st.c[,2], bc.health_care_pay_st.c[,2] ),
          
          p = list(c.expectations_index.c[,4], bc.expectations_index.c[,4], c.donate_lg_st.c[,4], bc.donate_lg_st.c[,4], 
                   c.health_care_groups_st.c[,4], bc.health_care_groups_st.c[,4], c.health_care_pay_st.c[,4], bc.health_care_pay_st.c[,4] ),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/expectations_1_nc_charity.tex",
          label = "tab:expectations_1_nc",column.sep.width = "1pt", table.placement = "!ht",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of LG CHP on Preferences for NGO Service Provision (No covariates)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", dep.var.caption = "", table.placement = "!ht",notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Index","Donation","Provision","Payment"), column.separate = c(2,2,2,2),
          add.lines = list(c("Restricted", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes")))


rm(list = grep("^(c|m)\\.", ls(), value = T))
rm(list = grep("^(bc|bm)\\.", ls(), value = T))

####################################################################################
####################################################################################
## Political Credit ####

# Unrestricted
for (i in names(dat[,c("ca_pres_index","performance_health_pres_st","performance_pres_st", "power_pres_st")])){ # Dependent Variables
  for (j in covariates) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("m",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = dat))
    # Output clustered SEs (county)
    assign(x = paste("c",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = dat),
                            cluster.vcov(lm(as.formula(model), data = dat), dat$village_id)))
  }
}

# Restricted
for (i in names(datb[,c("ca_pres_index","performance_health_pres_st","performance_pres_st", "power_pres_st")])){ # Dependent Variables
  for (j in covariatesb) {
    model = paste(i,"~","treatment","+", j)
    
    # Run each model
    assign(x = paste("bm",i,substr(j,1,1), sep = "."), 
           value = lm(as.formula(model), data = datb))
    # Output clustered SEs (county)
    assign(x = paste("bc",i,substr(j,1,1),sep = "."), 
           value = coeftest(lm(as.formula(model), data = datb),
                            cluster.vcov(lm(as.formula(model), data = datb), datb$village_id)))
  }
}

####################################################################################
####################################################################################
## Table L.40

stargazer(m.ca_pres_index.t, bm.ca_pres_index.t, m.power_pres_st.t, bm.power_pres_st.t, m.performance_health_pres_st.t, 
          bm.performance_health_pres_st.t,m.performance_pres_st.t, bm.performance_pres_st.t, 
          
          se = list(c.ca_pres_index.t[,2], bc.ca_pres_index.t[,2], c.power_pres_st.t[,2], bc.power_pres_st.t[,2], 
                    c.performance_health_pres_st.t[,2], bc.performance_health_pres_st.t[,2],c.performance_pres_st.t[,2], bc.performance_pres_st.t[,2] ),
          
          p = list(c.ca_pres_index.t[,4], bc.ca_pres_index.t[,4], c.power_pres_st.t[,4], bc.power_pres_st.t[,4], 
                   c.performance_health_pres_st.t[,4], bc.performance_health_pres_st.t[,4],c.performance_pres_st.t[,4], bc.performance_pres_st.t[,4] ),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/credit_pres_c_charity.tex",
          label = "tab:credit_pres_c", column.sep.width = "1pt",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of CHP Intervention on Credit to the President",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", dep.var.caption = "", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Index","Power","Health Performance","General Performance"), column.separate = c(2,2,2,2),
          add.lines = list(c("Restricted", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes")))


####################################################################################
####################################################################################
## Table L.41

stargazer(m.ca_pres_index.c, bm.ca_pres_index.c, m.power_pres_st.c, bm.power_pres_st.c, m.performance_health_pres_st.c, 
          bm.performance_health_pres_st.c,m.performance_pres_st.c, bm.performance_pres_st.c, 
          
          se = list(c.ca_pres_index.c[,2], bc.ca_pres_index.c[,2], c.power_pres_st.c[,2], bc.power_pres_st.c[,2], 
                    c.performance_health_pres_st.c[,2], bc.performance_health_pres_st.c[,2],c.performance_pres_st.c[,2], bc.performance_pres_st.c[,2] ),
          
          p = list(c.ca_pres_index.c[,4], bc.ca_pres_index.c[,4], c.power_pres_st.c[,4], bc.power_pres_st.c[,4], 
                   c.performance_health_pres_st.c[,4], bc.performance_health_pres_st.c[,4],c.performance_pres_st.c[,4], bc.performance_pres_st.c[,4] ),
          
          keep = c("treatment$"), 
          
          order = c("$treatment$"), covariate.labels=c("Treatment"),
          
          type = "latex", out = "tables/credit_pres_nc_charity.tex",
          label = "tab:credit_pres_nc", column.sep.width = "1pt",
          keep.stat = c("n"), dep.var.labels.include = F, no.space = T, model.numbers = T,
          title = "Effect of CHP Intervention on Credit to the President (No covariates)",
          notes = "Standard errors are clustered at the village level. $^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01", dep.var.caption = "", notes.align = "l", notes.append = F, notes.label = "",
          column.labels = c("Index","Power","Health Performance","General Performance"), column.separate = c(2,2,2,2),
          add.lines = list(c("Restricted", "No", "Yes", "No", "Yes", "No", "Yes", "No", "Yes")))

rm(list =  grep("^(c|m)\\.", ls(), value = T))
rm(list = grep("^(bc|bm)\\.", ls(), value = T))
